package function;

public class ChainMp3Impl implements ChainMp3 {

	private ChainMp3 previous;
	private ChainMp3 next;
	private Mp3File current;

	public ChainMp3Impl(Mp3File aCurrent, ChainMp3 aPrevious, ChainMp3 aNext) {
		super();
		this.previous = aPrevious;
		this.next = aNext;
		current = aCurrent;
	}

	public ChainMp3 getPrevious() {
		return previous;
	}

	public ChainMp3 getNext() {
		return next;
	}

	public Mp3File getMp3File() {
		return current;
	}

	public void setNext(ChainMp3 newMp3) {
		next = newMp3;
	}

	public void setPrevious(ChainMp3 newMp3) {
		previous = newMp3;
	}

	public int getLength() {
		int i = 1;
		ChainMp3 current = this;

		while (current.getNext() != null) {
			i++;
			current = current.getNext();
		}

		current = this;
		while (current.getPrevious() != null) {
			i++;
			current = current.getPrevious();
		}

		return i;
	}

	public boolean hasNext() {
		return (next != null);
	}

	public void setLast(Mp3File mp3File) {
		ChainMp3 current = this;

		while (current.getNext() != null) {
			current = current.getNext();
		}
		current.setNext(new ChainMp3Impl(mp3File, current, null));
	}
}
